<?php
//商家列表的切换菜单
class store_filter_nav_cache_auto_cache extends auto_cache{
	public function load($param)
	{
		$key = $this->build_key(__CLASS__,$param);
		//传入参数 city_id(城市)  cid(分类ID) tid(子分类ID) aid(行政区) qid(商圈ID)
//		$GLOBALS['fcache']->set_dir(APP_ROOT_PATH."public/runtime/data/".__CLASS__."/");
		$end_time = get_gmtime()-(3600*24);
		$result = unserialize($GLOBALS['db']->getOne("select cache_data from ".DB_PREFIX."auto_cache where cache_type = '".__CLASS__."' and cache_key = '".$key."' and cache_time > ".$end_time));
		if($result===false)
		{			
			$store_ctl=$param['ctl']!='index'?$param['ctl']:'index';//解决重写模式下 点击ktv列表右侧筛选条件 跳到餐厅列表中（2013-4-23-chh） 
			$GLOBALS['db']->query("delete from ".DB_PREFIX."auto_cache where cache_type = '".__CLASS__."' and cache_key = '".$key."'");
			$city_id = intval($param['city_id']);
			$url_param = $param;
			unset($url_param['city_id']);
			$area_id = intval($param['aid']);
			$quan_id = intval($param['qid']);
			$cate_id = intval($param['cid']);
			$deal_type_id = intval($param['tid']); 
			$purpose_id = intval($param['pid']);	
			$purpose_type_id = intval($param['sid']);
			$avg_price = intval($param['a']);
			$ext_takeaway ="" ;
			if(intval($param['takeaway']) == 1){
				$ext_takeaway = " AND takeaway=1 ";
			}
			
			//大区
			$area_list = $GLOBALS['db']->getAll("select * from ".DB_PREFIX."area where city_id = ".$city_id." and pid = 0 order by sort desc");
			$tmp_ar_url_param = $url_param;
			$area_all['name']	=	$GLOBALS['lang']['ALL'];
			$tmp_ar_url_param['aid'] = 0;
			$area_all['url']	=	url("store",$store_ctl,$tmp_ar_url_param);
			foreach($area_list as $k=>$v)
			{
					if($area_id==$v['id'])
					{
						$area_list[$k]['act'] = 1;
					}
					$tmp_url_param = $url_param;
					unset($tmp_url_param['qid']);
					$tmp_url_param['aid'] = $v['id'];
					$area_list[$k]['url'] = url("store",$store_ctl,$tmp_url_param);	
					$condition = build_deal_filter_condition($tmp_url_param,true);	
					
					$area_list[$k]['count'] = $GLOBALS['db']->getOne("select count(*) from ".DB_PREFIX."supplier_location where is_effect=1 $ext_takeaway $condition ");
			}
			$result['area_all'] = $area_all;
			$result['area_list'] = $area_list;
			
			//小区
			if($area_id>0)
			{			
				$quan_list = $GLOBALS['db']->getAll("select * from ".DB_PREFIX."area where city_id = ".$city_id." and pid = ".$area_id." order by sort desc");
				
				foreach($quan_list as $k=>$v)
					{
						if($quan_id==$v['id'])
						{
							$quan_list[$k]['act'] = 1;
						}
						$tmp_url_param = $url_param;
						$tmp_url_param['qid'] = $v['id'];
						$quan_list[$k]['url'] = url("store",$store_ctl,$tmp_url_param);	
						$condition = build_deal_filter_condition($tmp_url_param,true);						
						$quan_list[$k]['count'] = $GLOBALS['db']->getOne("select count(*) from ".DB_PREFIX."supplier_location where is_effect=1 $ext_takeaway $condition ");
				}			
			}
			$result['quan_list'] = $quan_list;
			
			//大类
			//输出分类
			$cate_list = $GLOBALS['db']->getAll("select dc.* from ".DB_PREFIX."deal_cate dc LEFT JOIN ".DB_PREFIX."deal_cate_city_link dccl ON dccl.cate_id=dc.id where dccl.city_id=$city_id AND dc.is_effect = 1 and dc.is_delete = 0 and dc.pid = 0 order by dc.sort desc");
			$tmp_c_url_param = $url_param;
			$cate_all['name']	=	$GLOBALS['lang']['ALL'];
			$tmp_c_url_param['cid'] = 0;
			$cate_all['url']	=	url("store",$store_ctl,$tmp_c_url_param);
			foreach($cate_list as $k=>$v)
			{
					if($cate_id==$v['id'])
					{
						$cate_list[$k]['act'] = 1;
					}
					$tmp_url_param = $url_param;
					unset($tmp_url_param['tid']);
					if($v['uname']!='')
					$tmp_url_param['cid'] = $v['uname'];
					else
					$tmp_url_param['cid'] = $v['id'];
					$cate_list[$k]['url'] = url("store",$store_ctl,$tmp_url_param);	
					$condition = build_deal_filter_condition($tmp_url_param,true);						
					$cate_list[$k]['count'] = $GLOBALS['db']->getOne("select count(*) from ".DB_PREFIX."supplier_location where is_effect=1 $ext_takeaway $condition ");
				
			}
			$result['cate_all'] = $cate_all;
			$result['cate_list'] = $cate_list;
			
			//小类		
			$deal_cate_id = $cate_id;
			if($deal_cate_id>0)
			{			
				$scate_list =$GLOBALS['db']->getAll("select t.id,t.name from ".DB_PREFIX."deal_cate_type as t left join ".DB_PREFIX."deal_cate_type_link as l on l.deal_cate_type_id = t.id LEFT JOIN ".DB_PREFIX."deal_cate_type_city_link dctcl ON dctcl.cate_type_id=t.id AND dctcl.cate_id=l.cate_id where dctcl.city_id=".intval($city_id)." AND l.cate_id = ".$deal_cate_id." order by t.sort desc");				
				foreach($scate_list as $k=>$v)
				{									
							if($deal_type_id==$v['id'])
							$scate_list[$k]['act'] = 1;
		
							$tmp_url_param = $url_param;
							$tmp_url_param['tid'] = $v['id'];
							$durl = url("store",$store_ctl,$tmp_url_param);						
							$scate_list[$k]['url'] = $durl;
							$condition = build_deal_filter_condition($tmp_url_param,true);
							$scate_list[$k]['count'] = $GLOBALS['db']->getOne("select count(*) from ".DB_PREFIX."supplier_location where is_effect=1 $ext_takeaway $condition ");
				
				}			
			}
			$result['scate_list'] = $scate_list;
			
			//目的筛选 
			$purpose_list = $GLOBALS['db']->getAll("SELECT pc.* FROM ".DB_PREFIX."purpose_cate pc LEFT JOIN ".DB_PREFIX."purpose_cate_city_link pccl ON pccl.cate_id=pc.id WHERE pccl.city_id=".$city_id." and pc.is_delete=0 group by pc.id ORDER BY pc.sort DESC, pc.id ASC");
			$tmp_p_url_param = $url_param;
			$purpose_all['name']	=	$GLOBALS['lang']['ALL'];
			$tmp_p_url_param['pid'] = 0;
			$purpose_all['url']	=	url("store",$store_ctl,$tmp_p_url_param);
			foreach($purpose_list as $k=>$v){
				if($purpose_id==$v['id'])
				{
					$purpose_list[$k]['act'] = 1;
				}
				$tmp_url_param = $url_param;
				unset($tmp_url_param['sid']);
				$tmp_url_param['pid'] = $v['id'];
				$purpose_list[$k]['url'] = url("store",$store_ctl,$tmp_url_param);	
				$condition = build_deal_filter_condition($tmp_url_param,true);					
				$purpose_list[$k]['count'] = $GLOBALS['db']->getOne("select count(*) from ".DB_PREFIX."supplier_location where is_effect=1 $ext_takeaway $condition ");
				
			}
			$result['purpose_all'] = $purpose_all;
			$result['purpose_list'] = $purpose_list;
			
			if($purpose_id > 0){
				$spurpose_list = $GLOBALS['db']->getAll("SELECT pct.* FROM ".DB_PREFIX."purpose_cate_type pct LEFT JOIN ".DB_PREFIX."purpose_cate_type_city_link pctcl ON pctcl.cate_type_id = pct.id WHERE pctcl.city_id=".$city_id." AND pct.cate_id=".$purpose_id." group by pct.id ORDER BY pct.sort DESC,pct.id ASC ");
				foreach($spurpose_list as $k=>$v)
				{									
						if($purpose_type_id==$v['id'])
							$spurpose_list[$k]['act'] = 1;
	
						$tmp_url_param = $url_param;
						$tmp_url_param['sid'] = $v['id'];
						$durl = url("store",$store_ctl,$tmp_url_param);						
						$spurpose_list[$k]['url'] = $durl;
						$condition = build_deal_filter_condition($tmp_url_param,true);
						$spurpose_list[$k]['count'] = $GLOBALS['db']->getOne("select count(*) from ".DB_PREFIX."supplier_location where is_effect=1 $ext_takeaway $condition ");
				
				}	
			}
			$result['spurpose_list'] = $spurpose_list;
			
			//人均
			$tmp_a_url_param = $url_param;
			$avg_price_all['name']	=	$GLOBALS['lang']['ALL'];
			$tmp_a_url_param['a'] = 0;
			$avg_price_all['url']	=	url("store",$store_ctl,$tmp_a_url_param);
			for($i=1;$i<=5;$i++)
			{
				if($avg_price==$i)
				{
					$avg_price_list[$i]['act'] = 1;
				}
				$tmp_url_param = $url_param;
				$tmp_url_param['a'] = $i;
				$durl = url("store",$store_ctl,$tmp_url_param);
				$avg_price_list[$i]['name'] = $GLOBALS['lang']['AVG_PRICE_'.$i];					
				$avg_price_list[$i]['url'] = $durl;
				$condition = build_deal_filter_condition($tmp_url_param,true);
				$avg_price_list[$i]['count'] = $GLOBALS['db']->getOne("select count(*) from ".DB_PREFIX."supplier_location where is_effect=1 $ext_takeaway $condition ");
		
			}
			$result['avg_price_all'] = $avg_price_all;
			$result['avg_price_list'] = $avg_price_list;
			
			//分组筛选
			$tag_group = $GLOBALS['db']->getAll("select g.* from ".DB_PREFIX."tag_group as g left join ".DB_PREFIX."tag_group_link as l on l.tag_group_id = g.id where l.category_id = ".$deal_cate_id." and g.allow_search = 1 order by g.sort desc");
			if($tag_group)
			{
				foreach($tag_group as $k=>$v)
				{
					$arr = explode(" ",$v['tags']);
					$group_tags = array();
					
					$tmp_a_url_param = $url_param;
					$tmp_a_url_param['g['.$v['id'].']'] = "";
					$tag_group[$k]['all'] = array("name"=>"全部","url"=>url("store","index",$tmp_a_url_param),"act"=>addslashes(trim($_REQUEST['g'][$v['id']]))==""?1:0);
					
					foreach($arr as $kk=>$vv)
					{						
						if(trim($vv)!="")
						{
							$group_tags[$kk]['name'] = $vv;
							$tmp_url_param = $url_param;
							$tmp_url_param['g['.$v['id'].']'] = $vv;
							$group_tags[$kk]['url'] = url("store",$store_ctl,$tmp_url_param);	
							if($vv==addslashes(trim($_REQUEST['g'][$v['id']])))
							{
								$group_tags[$kk]['act'] = 1;
							}	
						}
					}
					$tag_group[$k]["tags"] = $group_tags;
				}

				$result['tag_group'] = $tag_group;
			}		
			
			$db_data['cache_key'] = $key;
			$db_data['cache_type'] = __CLASS__;
			$db_data['cache_time'] = get_gmtime();
			$db_data['cache_data'] = serialize($result);
			$GLOBALS['db']->autoExecute(DB_PREFIX."auto_cache",$db_data);
		
		}	

		return $result;
	}
	public function rm($param)
	{
		$key = $this->build_key(__CLASS__,$param);
		$GLOBALS['db']->query("delete from ".DB_PREFIX."auto_cache where cache_type = '".__CLASS__."' and cache_key = '".$key."'");
	}
	public function clear_all()
	{
		$GLOBALS['db']->query("delete from ".DB_PREFIX."auto_cache where cache_type = '".__CLASS__."'");		
	}
}
?>